// src/main/java/cn/edu/cdu/health/controller/DepartmentController.java
package cn.edu.cdu.health.controller;

import cn.edu.cdu.health.entity.Department;
import cn.edu.cdu.health.service.IDepartmentService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/departments")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @GetMapping("/page")
    public ResponseEntity<IPage<Department>> page(
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String keyword) {
        IPage<Department> page = departmentService.getDepartmentPage(current, size, keyword);
        return ResponseEntity.ok(page);
    }

    @GetMapping("/active")
    public ResponseEntity<List<Department>> getActiveDepartments() {
        List<Department> departments = departmentService.getActiveDepartments();
        return ResponseEntity.ok(departments);
    }

    @GetMapping("/code/{deptCode}")
    public ResponseEntity<Department> getByCode(@PathVariable String deptCode) {
        Department department = departmentService.getByDeptCode(deptCode);
        return department != null ? ResponseEntity.ok(department) : ResponseEntity.notFound().build();
    }

    @PostMapping
    public ResponseEntity<?> create(@RequestBody Department department) {
        // 检查科室编码是否已存在
        if (departmentService.isDeptCodeExists(department.getDeptCode())) {
            return ResponseEntity.badRequest().body("科室编码已存在");
        }

        boolean result = departmentService.save(department);
        return result ? ResponseEntity.ok("创建成功") : ResponseEntity.badRequest().body("创建失败");
    }

    @PutMapping("/{id}")
    public ResponseEntity<?> update(@PathVariable Long id, @RequestBody Department department) {
        department.setId(id);
        boolean result = departmentService.updateById(department);
        return result ? ResponseEntity.ok("更新成功") : ResponseEntity.badRequest().body("更新失败");
    }

    @PutMapping("/{id}/status")
    public ResponseEntity<?> updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        try {
            boolean result = departmentService.updateStatus(id, status);
            return result ? ResponseEntity.ok("状态更新成功") : ResponseEntity.badRequest().body("状态更新失败");
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> delete(@PathVariable Long id) {
        boolean result = departmentService.logicDelete(id);
        return result ? ResponseEntity.ok("删除成功") : ResponseEntity.badRequest().body("删除失败");
    }

    @GetMapping("/check-code")
    public ResponseEntity<Boolean> checkDeptCode(@RequestParam String deptCode) {
        boolean exists = departmentService.isDeptCodeExists(deptCode);
        return ResponseEntity.ok(exists);
    }
}